/**
 * Created with IntelliJ IDEA.
 * User: duoba
 * Date: 16/2/23
 * Time: 下午4:18
 * To change this template use File | Settings | File Templates.
 */

package com.cake21.uitest.System.Core;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import com.cake21.uitest.System.Map.ConfigMap;

public final class Server extends Thread {
    /*程序主入口*/
    public static void main(String[] args) {
        Server.commander = new Commander();
        commander.dispatch();
    }

    /*指挥官*/
    private static Commander commander = null;

    /*返回指挥官*/
    public static Commander getCommander() {
        return commander;
    }

    /*运行模式*/
    private static String runMode = initRunMode();

    /*返回运行模式*/
    public static String getRunMode() {
        return runMode;
    }

    /*初始化运行模式*/
    private static String initRunMode() {
        String runMode = "local";
        try {
            // 读取配置错误时，默认设为local
            runMode = ConfigMap.getMap().get("normal").get("mode");
            runMode = runMode.equals("") ? "local" : runMode;
        } catch (Exception e) {
            // 配置错误
            e.printStackTrace();
        }
        return runMode;
    }

    /*页面超时时间(s)*/
    private static Integer pageLoadTimeout = initPageLoadTimeout();

    /*返回页面超时时间*/
    public static Integer getPageLoadTimeout() {
        return pageLoadTimeout;
    }

    /*初始化页面超时时间*/
    private static Integer initPageLoadTimeout() {
        Integer pageLoadTimeout = 30;
        try {
            // 读取配置错误时，默认设为30秒
            pageLoadTimeout = Integer.parseInt(ConfigMap.getMap().get("timeout").get("page"));
        } catch (Exception e) {
            // 配置错误
            e.printStackTrace();
        }
        return pageLoadTimeout;
    }

    /*脚本超时时间(s)*/
    private static Integer scriptTimeout = initScriptTimeout();

    /*返回脚本超时时间*/
    public static Integer getScriptTimeout() {
        return scriptTimeout;
    }

    /*初始化脚本超时时间*/
    private static Integer initScriptTimeout() {
        Integer scriptTimeout = 30;
        try {
            // 读取配置错误时，默认设为30秒
            scriptTimeout = Integer.parseInt(ConfigMap.getMap().get("timeout").get("script"));
        } catch (Exception e) {
            // 配置错误
            e.printStackTrace();
        }
        return scriptTimeout;
    }

    /*等待超时时间(s)*/
    private static Integer implicitlyWait = initImplicitlyWait();

    /*返回等待超时时间*/
    public static Integer getImplicitlyWait() {
        return implicitlyWait;
    }

    /*初始化等待超时时间*/
    private static Integer initImplicitlyWait() {
        Integer implicitlyWait = 10;
        try {
            // 读取配置错误时，默认设为10秒
            implicitlyWait = Integer.parseInt(ConfigMap.getMap().get("timeout").get("implicitness"));
        } catch (Exception e) {
            // 配置错误
            e.printStackTrace();
        }
        return implicitlyWait;
    }

    /*唯一标识号*/
    public static String generateId() {
        String time = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
        String rand = String.format("%04d", new Random().nextInt(9999));
        return time + "-" + rand;
    }

    /*当前时间*/
    public static String currentTime() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }
}
